package view
{
	import flash.events.Event;
	
	import model.AnneeEtude;
	import model.AnneeUniversitaire;
	import model.FaireParti;
	import model.Formation;
	import model.Matiere;
	import model.Semestre;
	import model.Ue;
	import model.proxy.SemestreProxy;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import view.components.SemestrePanel;
	
	public class SemestrePanelMediator extends Mediator implements IMediator
	{
		public static const NAME : String = 'SemestrePanelMediator'; 
		
		/** 
		 * Constructor 
		 * Listen event of the component to send notification 
		 */ 
		public function SemestrePanelMediator(viewComponent : SemestrePanel)
		{
			super(NAME, viewComponent);
			viewComponent.addEventListener(SemestrePanel.OBTENIR_LISTE_ANNEESUNIVERSITAIRE, ObtenirListeAnneeUniversitaireNotification);
			viewComponent.addEventListener(SemestrePanel.OBTENIR_LISTE_ANNEES_ETUDE_SEMESTRE, ObtenirListeAnneesEtudeSemestreNotification);
			viewComponent.addEventListener(SemestrePanel.OBTENIR_LISTE_SEMESTRE, ObtenirListeSemestreNotification);
			viewComponent.addEventListener(SemestrePanel.VALIDER_AJOUT_UE_SEMESTRE, ValiderAjoutUESemestreNotification);
			viewComponent.addEventListener(SemestrePanel.VALIDER_MODIF_UE_SEMESTRE, ValiderModifUESemestreNotification);
			
			viewComponent.addEventListener(SemestrePanel.OBTENIR_LISTE_MATIERES, ObtenirListeMatieresNotification);
			viewComponent.addEventListener(SemestrePanel.OBTENIR_LISTE_MATIERES_ASS, ObtenirListeMatieresAssNotification);
			viewComponent.addEventListener(SemestrePanel.SUPPRIMER_UE_SEMESTRE, SuprimerUESemestreNotification);
			viewComponent.addEventListener(SemestrePanel.VALIDER_AJOUT_MATIERE, AjouterMatiereNotification);
			viewComponent.addEventListener(SemestrePanel.VALIDER_MODIF_MATIERE, ModifierMatiereNotification);
			viewComponent.addEventListener(SemestrePanel.VALIDER_MODIF_MATIERE_ASS, ModifierMatiereAssNotification);
			viewComponent.addEventListener(SemestrePanel.SUPPRIMER_MATIERE, SupprimerMatiereNotification);
			viewComponent.addEventListener(SemestrePanel.SUPPRIMER_MATIERE_ASS, SupprimerMatiereAssNotification);
			viewComponent.addEventListener(SemestrePanel.DRAG_AND_DROP_UE, DragAndDropUeNotification);
		}
		
		/** 
		 * Send a formation notification with a formationVO loaded with the values entered 
		 */ 
		 private function ObtenirListeAnneeUniversitaireNotification(event : Event=null):void 
		 { 
			sendNotification(SemestrePanel.OBTENIR_LISTE_ANNEESUNIVERSITAIRE, null); 
		 } 
		 private function ObtenirListeAnneesEtudeSemestreNotification(event : Event=null):void 
		 { 
		 	var anneeUniversitaire:AnneeUniversitaire = semestrePanel.annee_universitaire.selectedItem.valueOf();
		 	var formation:Formation = (semestrePanel.formation.selectedItem.valueOf() as FaireParti).aFormation;
			sendNotification(SemestrePanel.OBTENIR_LISTE_ANNEES_ETUDE_SEMESTRE, [anneeUniversitaire, formation]);
		 } 
		 private function ObtenirListeSemestreNotification(event : Event=null):void 
		 { 
		 	var anneeEtude:AnneeEtude = semestrePanel.annee_etude.selectedItem.valueOf();
			sendNotification(SemestrePanel.OBTENIR_LISTE_SEMESTRE, anneeEtude);
		 } 
		 private function ValiderAjoutUESemestreNotification(event : Event=null):void 
		 { 
		 	var semestre:Semestre = semestrePanel.semestreSelected;
		 	var ue:Ue = new Ue;
		 	ue.nom = semestrePanel.nomUE.text;
		 	ue.nb_ects = parseInt(semestrePanel.nb_ectsUE.text);
		 	ue.note_min = parseInt(semestrePanel.note_minUE.text);
		 	ue.couleur = semestrePanel.couleurUE.selectedColor.toString();
		 	ue.objectifs = semestrePanel.objectifsUE.text; 
		 	var listeMatieresAss:ArrayCollection = semestrePanel.DataGridMatiereAssociees.dataProvider as ArrayCollection;
			sendNotification(SemestrePanel.VALIDER_AJOUT_UE_SEMESTRE, [semestre, ue, listeMatieresAss]);
		 }
		 private function ValiderModifUESemestreNotification(event : Event=null):void 
		 { 
		 	var semestre:Semestre = semestrePanel.semestreSelected;
		 	var ue:Ue = semestrePanel.ueSelected;
		 	ue.nom = semestrePanel.nomUE.text;
		 	ue.nb_ects = parseInt(semestrePanel.nb_ectsUE.text);
		 	ue.note_min = parseInt(semestrePanel.note_minUE.text);
		 	ue.couleur = semestrePanel.couleurUE.selectedColor.toString();
		 	ue.objectifs = semestrePanel.objectifsUE.text;
		 	var listeMatieresAss:ArrayCollection = semestrePanel.DataGridMatiereAssociees.dataProvider as ArrayCollection; 
			sendNotification(SemestrePanel.VALIDER_MODIF_UE_SEMESTRE, [semestre, ue, listeMatieresAss]);
		 }
		 private function ObtenirListeMatieresNotification(event : Event=null):void 
		 { 
		 	var anneeEtude:AnneeEtude = semestrePanel.annee_etude.selectedItem.valueOf();
			sendNotification(SemestrePanel.OBTENIR_LISTE_MATIERES, anneeEtude);
		 }
		 private function ObtenirListeMatieresAssNotification(event : Event=null):void 
		 { 
		 	var ue:Ue = semestrePanel.ueSelected;
			sendNotification(SemestrePanel.OBTENIR_LISTE_MATIERES_ASS, ue);
		 }
		 private function SuprimerUESemestreNotification(event : Event=null):void 
		 { 
		 	var semestre:Semestre = semestrePanel.semestreSelected;
		 	var ue:Ue = semestrePanel.ueSelected;
			sendNotification(SemestrePanel.SUPPRIMER_UE_SEMESTRE, [semestre, ue]);
		 }
		 private function AjouterMatiereNotification(event : Event=null):void 
		 { 
		 	var anneeEtude:AnneeEtude = semestrePanel.annee_etude.selectedItem.valueOf() as AnneeEtude;
		 	var matiere:Matiere = new Matiere;
		 	matiere.nom = semestrePanel.nom_matiere.text;
		 	matiere.desc_objectifs = semestrePanel.desc_objectifs_matiere.text;
			sendNotification(SemestrePanel.VALIDER_AJOUT_MATIERE, [anneeEtude, matiere]);
		 }
		 private function ModifierMatiereNotification(event : Event=null):void 
		 { 
		 	var anneeEtude:AnneeEtude = semestrePanel.annee_etude.selectedItem.valueOf() as AnneeEtude;
		 	var matiere:Matiere = semestrePanel.dataGridMatiere.selectedItem.valueOf() as Matiere;
		 	matiere.nom = semestrePanel.nom_matiere.text;
		 	matiere.desc_objectifs = semestrePanel.desc_objectifs_matiere.text;
			sendNotification(SemestrePanel.VALIDER_MODIF_MATIERE, [anneeEtude, matiere]);
		 }
		 private function ModifierMatiereAssNotification(event : Event=null):void 
		 { 
		 	var ue:Ue = semestrePanel.ueSelected;
		 	var matiere:Matiere = semestrePanel.DataGridMatiereAssociees.selectedItem.valueOf() as Matiere;
		 	matiere.nom = semestrePanel.nom_matiere.text;
		 	matiere.desc_objectifs = semestrePanel.desc_objectifs_matiere.text;
			sendNotification(SemestrePanel.VALIDER_MODIF_MATIERE_ASS, [ue, matiere]);
		 }
		 private function SupprimerMatiereNotification(event : Event=null):void 
		 { 
		 	var anneeEtude:AnneeEtude = semestrePanel.annee_etude.selectedItem.valueOf() as AnneeEtude;
		 	var matiere:Matiere = semestrePanel.dataGridMatiere.selectedItem.valueOf() as Matiere;
			sendNotification(SemestrePanel.SUPPRIMER_MATIERE, [anneeEtude, matiere]);
		 }
		 private function SupprimerMatiereAssNotification(event : Event=null):void 
		 { 
		 	var ue:Ue = semestrePanel.ueSelected;
		 	var matiere:Matiere = semestrePanel.DataGridMatiereAssociees.selectedItem.valueOf() as Matiere;
			sendNotification(SemestrePanel.SUPPRIMER_MATIERE_ASS, [ue, matiere]);
		 }
		 private function DragAndDropUeNotification(event : Event=null):void 
		 { 
		 	var semestre1:Semestre = semestrePanel.semestre1;
		 	var semestre2:Semestre = semestrePanel.semestre2;
		 	var ue:Ue = semestrePanel.ueSelected;
			sendNotification(SemestrePanel.DRAG_AND_DROP_UE, [semestre1, semestre2, ue]);
		 }
		 
		 /** 
		 * Notifications recieved by SemestreProxy 
		 */ 
		 override public function listNotificationInterests():Array 
		 { 
		 	return [  
		 			SemestreProxy.LISTE_ANNEES_UNIVERSITAIRE_SEMESTRE_OBTENUE,
		 			SemestreProxy.LISTE_ANNEES_ETUDE_SEMESTRE_OBTENUE,
		 			SemestreProxy.LISTE_SEMESTRES_OBTENUE,
		 			SemestreProxy.LISTE_MATIERES_OBTENUE,
		 			SemestreProxy.LISTE_MATIERES_ASS_OBTENUE
		 			]; 
		 } 
		 
		 override public function handleNotification(note : INotification):void 
		 { 
			 switch (note.getName()) 
			 { 
			 	case SemestreProxy.LISTE_ANNEES_UNIVERSITAIRE_SEMESTRE_OBTENUE:
			 	{
			 		semestrePanel.annee_universitaire.dataProvider = note.getBody() as Array;
			 		semestrePanel.changeAnneeEtude(null);
			 		break;
			 	}
			 	case SemestreProxy.LISTE_ANNEES_ETUDE_SEMESTRE_OBTENUE:
			 	{
			 		semestrePanel.annee_etude.dataProvider = note.getBody() as Array;
			 		semestrePanel.onChangeComboAnneeEtude(null);
			 		break;
			 	}
			 	case SemestreProxy.LISTE_SEMESTRES_OBTENUE:
			 	{
			 		var sem1:Semestre = (note.getBody() as Array)[0] as Semestre;
			 		var sem2:Semestre = (note.getBody() as Array)[1] as Semestre;
			 		semestrePanel.semestre1 = sem1;
			 		semestrePanel.semestre2 = sem2;
			 		semestrePanel.dataGridSem1.dataProvider = sem1.collUes as Array;
					semestrePanel.dataGridSem2.dataProvider = sem2.collUes as Array;
					semestrePanel.nbSemaineSem1Text.text = sem1.nb_semaine.toString();
					semestrePanel.nbSemaineSem2Text.text = sem2.nb_semaine.toString();
			 		break;
			 	}
			 	case SemestreProxy.LISTE_MATIERES_OBTENUE:
			 	{
			 		semestrePanel.dataGridMatiere.dataProvider = note.getBody() as Array;			 	
			 		break;
			 	}
			 	case SemestreProxy.LISTE_MATIERES_ASS_OBTENUE:
			 	{
			 		semestrePanel.DataGridMatiereAssociees.dataProvider = note.getBody() as Array;			 	
			 		break;
			 	}
			 	default:
			 	break;
		 	} 
		 } 
		  
		 private function get semestrePanel():SemestrePanel 
		 { 
		 	return viewComponent as SemestrePanel; 
		 }
	}
}

